﻿using System;
using System.Data;
using System.Data.SqlClient;

public   class LoginDAL
{
    dbConnection db = new dbConnection();
	public string GetPassword(string UserName)
	{
		SqlConnection cn = null;
		SqlCommand cmd = null;
		string Counter = string.Empty;

		dbConnection db = new dbConnection();

		try
		{
			cn = new SqlConnection();
			cn.ConnectionString = db.GetCS();

			cmd = new SqlCommand();
			cmd.Connection = cn;
			cmd.CommandType = CommandType.Text;
			cmd.CommandText = "Select Password FROM Registrations WHERE (USERNAME=@UserName) ";



			cmd.Parameters.AddWithValue("@UserName", UserName);
			//cmd.Parameters.AddWithValue("@Password", Password);

			cn.Open();
			Counter = Convert.ToString(cmd.ExecuteScalar());

			if (Counter == "")
			{
				string p = "";
				return (p);
			}


		}
		catch (Exception ex)
		{
			throw new Exception(ex.Message);
		}
		finally
		{
			if (cn != null)
			{
				cn.Close();
				cn.Dispose();
				cn = null;
			}
		}
	return (Counter);
	}
    public  string CheckCredentials(string UserName, string Password)
    {
          SqlConnection cn = null;
          SqlCommand cmd = null;
          string Counter = string.Empty;

          dbConnection db = new dbConnection();

          try
          {
              cn = new SqlConnection();
              cn.ConnectionString = db.GetCS();

              cmd = new SqlCommand();
              cmd.Connection = cn;
              cmd.CommandType = CommandType.Text;
              cmd.CommandText = "Select AccountId FROM Registrations WHERE (USERNAME=@UserName) ";



              cmd.Parameters.AddWithValue("@UserName", UserName);
              //cmd.Parameters.AddWithValue("@Password", Password);

              cn.Open();
              Counter = Convert.ToString(cmd.ExecuteScalar());

              if (Counter == "")
              {
                  string p = "";
                  return (p);
              }

            
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message);
          }
          finally
          {
              if (cn != null)
              {
                  cn.Close();
                  cn.Dispose();
                  cn = null;
              }
          }
          return (Counter);
          }

    
    public  int UnSubscribeMe(LoginBO Login)
    {
        SqlConnection cn = null;
        SqlCommand cmd = null;
        int Counter = 0;

        try
        {
            cn = new SqlConnection();
            cn.ConnectionString = db.GetCS();
            cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Delete UserName from [Registrations] where UserName=@UserName";

            cmd.Parameters.AddWithValue("@UserName", Login.UserName);

            cn.Open();
            Counter = cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            if (cn != null)
            {
                cn.Close();
                cn.Dispose();
                cn = null;
            }
        }
        return (Counter);
    }

  }
    

